package org.jeecg.modules.parameter.biz.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.parameter.api.entity.SimLine;
import org.jeecg.modules.parameter.api.entity.line.SimLinePre;
import org.jeecg.modules.parameter.biz.mapper.SimLineMapper;
import org.jeecg.modules.parameter.biz.service.SimLineService;
import org.jeecg.modules.parameter.common.biz.service.impl.FzServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LSY
 */
@Slf4j
@Primary
@Service
@Transactional(rollbackFor = Exception.class)
public class SimLineServiceImpl extends FzServiceImpl<SimLineMapper, SimLine> implements SimLineService {

    @Resource
    private SimLineMapper mapper;

    @Override
    public List<SimLine> getAllLine() {
        return mapper.getAllLine();
    }


    @Override
    public String saveLineParameter(SimLine simLine) {
        // check

        String uuid = UUIDGenerator.generate();
        simLine.setId(uuid);
        LoginUser user = getCurrentUser();
        String currentTime = getCurrentTime();
        simLine.setCreatedBy(user.getId());
        simLine.setCreatedAt(currentTime);
//        simLine.setModifiedBy(user.getId());
//        simLine.setModifiedAt(currentTime);
        if (mapper.insert(simLine) > 0) {
            return uuid;
        }
        return null;
    }

    @Override
    public Boolean updateLineParameter(SimLine simLine) {
        LoginUser user = getCurrentUser();
        String currentTime = getCurrentTime();
        simLine.setModifiedBy(user.getId());
        simLine.setCreatedAt(currentTime);
        return mapper.updateByUuId(simLine);
    }


    @Override
    public Boolean checkLineId(String lineUuid, String lineId) {
        return !mapper.getLineByLineId(lineUuid, lineId).isEmpty();
    }


    @Override
    public Boolean updateSortNo(List<SimLine> lines) {
        return reorderLines(lines);
    }

    @Override
    public List<SimLine> getAllLineColor() {

        return mapper.getAllLineColor();
    }

    @Override
    public String getLineColorByLineId(String lineId) {
        List<SimLine> list = mapper.getLineColorByLineId(lineId);
        String tempLineColor = null;
        for (SimLine line : list) {
            tempLineColor = line.getColor();
        }
        return tempLineColor;
    }

    @Override
    public List<SimLinePre> selectPre(String lineId) {
        return mapper.selectPre(lineId);
    }

    //    数据排序序号更新
    public Boolean reorderLines(List<SimLine> lines) {

        if (lines == null || lines.size() <= 1) {
            return false;
        }
        for (int i = 0; i < lines.size(); i++) {
            SimLine line = lines.get(i);
            line.setSortNo(i); // 从0开始，依次加一
            mapper.updateBatch(line);
        }
        return true;
    }


    private LoginUser getCurrentUser() {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            user = new LoginUser();
            user.setId("admin");
        }
        return user;
    }

    private String getCurrentTime() {
        return DateUtils.now();
    }


}
